<template>
  <div>
    <el-card>
      功能清单:
      <el-form :model="tableForm" ref="tableForm" size="small" :inline="true" label-width="100px">
        <el-form-item label="所属模块" prop="moduleName">
          <el-input placeholder="请输入模块名称，必须为英文" v-model="tableForm.moduleName"></el-input>
        </el-form-item>
        <el-form-item label="日志功能" prop="logFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.logFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="鉴权功能" prop="authFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.authFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="查询功能" prop="searchFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.searchFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="分页功能" prop="pageFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.pageFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="列表功能" prop="listFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.listFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="导出功能" prop="exportFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.exportFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="详情功能" prop="detailFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.detailFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="添加功能" prop="insertFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.insertFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="修改功能" prop="updateFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.updateFlag"></el-checkbox>
        </el-form-item>
        <el-form-item label="删除功能" prop="deleteFlag">
          <el-checkbox :true-label="1" :false-label="0" v-model="tableForm.deleteFlag"></el-checkbox>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table ref="dragTable" :inline="true" label-width="68px" :data="tableColumns" row-key="columnId">
        <el-table-column label="序号" fixed type="index" prop="columnId" :min-width="50" class-name="allowDrag" />
        <el-table-column
          label="字段列名"
          prop="columnName"
          :min-width="100"
          fixed
        />
        <el-table-column label="字段描述" prop="remarks" :show-overflow-tooltip="true"/>
        <el-table-column
          label="物理类型"
          prop="dbType"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="Java类型" :min-width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.javaType" placeholder="Java类型">
              <el-option v-for="sysEnum in sysEnum.dbAndJavaRelEnum" :key="sysEnum.code" :label="sysEnum.describStr" :value="sysEnum.code" />
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="最大长度" prop="maxLength" ></el-table-column>

        <el-table-column label="主键">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.primaryKeyFlag"/>
          </template>
        </el-table-column>

        <el-table-column label="索引">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.indexFlag"/>
          </template>
        </el-table-column>

        <el-table-column label="可为空">
          <template slot-scope="scope">
            <el-checkbox :true-label="1" :false-label="0" v-model="scope.row.forceAvaFlag" :disabled = "scope.row.nullAvaFlag == 0"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="查询">
          <template slot-scope="scope">
            <el-checkbox :true-label="1" :false-label="0" v-model="scope.row.searchFlag" @change = "checkTableFormValue($event,'searchFlag')"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="列表">
          <template slot-scope="scope">
            <el-checkbox :true-label="1" :false-label="0" v-model="scope.row.listFlag" @change = "checkTableFormValue($event,'listFlag')"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="导出">
          <template slot-scope="scope">
            <el-checkbox :true-label="1" :false-label="0" v-model="scope.row.exportFlag" @change = "checkTableFormValue($event,'exportFlag')"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="详情">
          <template slot-scope="scope">
            <el-checkbox :true-label="1" :false-label="0" v-model="scope.row.detailFlag" @change = "checkTableFormValue($event,'detailFlag')"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="插入">
          <template slot-scope="scope">
            <el-checkbox :true-label="1" :false-label="0" v-model="scope.row.insertFlag" @change = "checkTableFormValue($event,'insertFlag')"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="编辑">
          <template slot-scope="scope">
            <el-checkbox :true-label="1" :false-label="0" v-model="scope.row.updateFlag" @change = "checkTableFormValue($event,'updateFlag')"></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="查询方式" :min-width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.searchType" placeholder="查询方式">
              <el-option v-for="sysEnum in sysEnum.searchTypeEnum" :key="sysEnum.code" :label="sysEnum.describStr" :value="sysEnum.code" />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="显示类型" :min-width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.componentName" placeholder="显示类型">
              <el-option v-for="sysEnum in sysEnum.forceComponentEnum" :key="sysEnum.code" :label="sysEnum.describStr" :value="sysEnum.code" />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="字典类型" :min-width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.dictType" clearable filterable placeholder="请选择">
              <el-option
                v-for="dict in dictOptions"
                :key="dict.dictType"
                :label="dict.dictName"
                :value="dict.dictType">
                <span style="float: left">{{ dict.dictName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.dictType }}</span>
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="枚举类型" :min-width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.enumType" clearable filterable placeholder="请选择">
              <el-option
                v-for="enumValue in enumOptions"
                :key="enumValue"
                :label="enumValue"
                :value="enumValue">
                <span style="float: left">{{ enumValue }}</span>
              </el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>
      <el-form label-width="100px">
        <el-form-item style="text-align: center;margin-left:-100px;margin-top:10px;">
          <el-button type="primary" @click="submitForm()">提交</el-button>
          <el-button @click="close()">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {listTableColumn, getGenTableInfo,editGenTable} from "@/api/tool/cusGen";
import {findEnumKey} from "@/api/system/sysEnum/sysEnum.js";
import { optionselect as getDictOptionselect } from "@/api/system/dict/type";

export default {
  name: "GenTableColumn",
  dicts: ['sys_yes_no','sys_normal_open_status'],
  sysEnums:['searchTypeEnum', 'dbAndJavaRelEnum','forceComponentEnum'],
  data() {
    return {
      tableForm:{
      },
      // 选中选项卡的 name
      activeName: "columnInfo",
      // 表列信息
      tableColumns: [],
      // 字典信息
      dictOptions: [],
      // 枚举信息
      enumOptions: [],
      // 菜单信息
      menus: [],
      // 表详细信息
      info: {},
      tableId: undefined
    };
  },
  watch:{
    tableForm:{
      handler(newVal,oldVal){
        if (newVal.insertFlag == 1){
          if (!this.findTableHasProp('insertFlag')){
            this.$modal.msgError("需先勾选一个插入字段");
            newVal.insertFlag = 0
          }
        }
        if (newVal.updateFlag == 1){
          if (!this.findTableHasProp('updateFlag')){
            this.$modal.msgError("需先勾选一个更新字段");
            newVal.updateFlag = 0
          }
        }
        if (newVal.detailFlag == 1){
          if (!this.findTableHasProp('detailFlag')){
            this.$modal.msgError("需先勾选一个详情字段");
            newVal.detailFlag = 0
          }
        }
        if (newVal.listFlag == 1){
          if (!this.findTableHasProp('listFlag')){
            this.$modal.msgError("需先勾选一个列表字段");
            newVal.listFlag = 0
          }
        }
        if (newVal.exportFlag == 1){
          if (!this.findTableHasProp('exportFlag')){
            this.$modal.msgError("需先勾选一个导出字段");
            newVal.exportFlag = 0
          }
        }
        if (newVal.searchFlag == 1){
          if (!this.findTableHasProp('searchFlag')){
            this.$modal.msgError("需先勾选一个查询字段");
            newVal.searchFlag = 0
          }
        }
      },
      deep: true,
      immediate: true,
    }
  },
  created() {
    const tableId = this.$route.params && this.$route.params.tableId;
    if (tableId) {
      this.tableId = tableId
      // 获取表详细信息
      this.getList();
      /** 查询字典下拉列表 */
      getDictOptionselect().then(res => {
        this.dictOptions = res;
      });
      findEnumKey().then(res=>{
        this.enumOptions = res;
      })
    }
  },
  methods: {
    /** 查询表信息 */
    getTableInfo(){
      getGenTableInfo(this.tableId).then(res=>{
        this.tableForm = {
          moduleName: res.moduleName,
          logFlag: res.logFlag,
          authFlag: res.authFlag,
          insertFlag: res.insertFlag,
          updateFlag: res.updateFlag,
          detailFlag: res.detailFlag,
          listFlag: res.listFlag,
          exportFlag: res.exportFlag,
          searchFlag: res.searchFlag,
          pageFlag: res.pageFlag,
          deleteFlag: res.deleteFlag
        }
      })
    },
    /** 查询数据源列表 */
    getList() {
      this.loading = true;
      var param = {
        tableId: this.tableId
      }
      listTableColumn(param).then(res => {
          this.tableColumns = res;
          //获取表信息
          this.getTableInfo()

        }
      ).finally(()=>{
        this.loading = false;
      })
    },
    //查询表中是否存在某个属性
    findTableHasProp(propName){
      for (const item of this.tableColumns) {
        if (item[propName] == 1){
          return true;
        }
      }
      return false;
    },
    //查询表中功能是否要取消
    checkTableFormValue(val,propName){
      if (val == 0 && !(this.findTableHasProp(propName))){
        this.tableForm[propName] = 0
      }
    },
    /** 提交按钮 */
    submitForm() {
      var param ={
        ...this.tableForm
      }
      param.tableId = this.tableId
      param.genTableColumnEditRoList = this.tableColumns
      this.loading = true;
      editGenTable(param)
        .then(res=>{
          this.$modal.msgSuccess("操作成功");
          this.$router.go(-1);
        })
        .finally(res=>{
          this.loading = false;
        })
    },
    /** 关闭按钮 */
    close() {
      this.$router.go(-1)
    }
  }
};
</script>
